using System;
using System.Threading;
using System.Windows.Forms;
using SpellCenter.Core;

namespace SpellCenter.UI
{
    public partial class FileIndexingDialog : Form, ITaskMonitor, IIndexingManager, ISpellingChangeManager
    {

        public FileIndexingDialog()
        {
            InitializeComponent();
            _cancelButton.Click += CancelButton_Click;
            _progressBar.Step = 1;
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
          CancelTask();
            _cancelButton.Text = "Cancelling...";
            _cancelButton.Enabled = false;
        }

        #region IIndexStatusIndicator Members

        private delegate void IntSetterMethod(int value);
        private void ProgressBar_SetMaximum(int value)
        {
            _progressBar.Value = 0;
            _progressBar.Maximum = value;
        }

        private void DisplayStatus()
        {
            _progressBar.PerformStep();
            _jobLabel.Text = StepDescription;
            Refresh();
        }

        #region IIndexingManager Members

        public CorpusIndex BeginIndexing(FilesInCorpusList config)
        {
            CorpusIndex index = new CorpusIndex(config, this);

            //ThreadStart jobstart = index.DoIndexing;
            //new Thread(jobstart).Start();
            return index;
        }


        private void ActivateCancelButton()
        {
            _cancelButton.Text = "Cancel";
            _cancelButton.Enabled = true;
        }

      private void InitializeProgressbar()
        {
            _progressBar.Value = 0;
            _progressBar.Maximum = 0;
        }

        public event EventHandler Cancelled;
        protected virtual void OnCancelled(EventArgs e)
        {
            EventHandler handler = Cancelled;
            if(handler != null)
                handler(this, e);
        }

        public event EventHandler<EventArgs> Finished;
        protected virtual void OnFinished(EventArgs e)
        {
            EventHandler<EventArgs> handler = Finished;
            if (handler != null)
                handler(this, e);
        }

        #endregion

        #endregion

        #region ISpellingChangeManager Members

        public void ChangeSpelling(CorpusIndex index, string word, string newWord)
        {
//            ThreadStart threadstart = delegate {index.ReplaceAllWords(word, newWord, this);};
//            Thread thread = new Thread(threadstart);
//            thread.Name = "Replace All Thread";
//            thread.IsBackground = true;
//            thread.Start();
            index.ReplaceAllWords(word, newWord);
        }

        #endregion

        #region ITaskMonitor Members

        public int StepCount
        {
          get
          {
            return _progressBar.Maximum;
          }
          private set
          {
            _progressBar.Invoke(new IntSetterMethod(ProgressBar_SetMaximum), value);
          }
        }

      private int _completedSubTasksCount;
        public int StepsCompletedCount
        {
          get
          {
            return _completedSubTasksCount;
          }
          private set
          {
            _completedSubTasksCount = value;
          }
        }

      private bool _cancelled;
        public bool IsCancelled
        {
          get
          {
            return _cancelled;
          }
          private set
          {
            _cancelled = value;
          }
        }

        #endregion

        #region ITaskMonitor Members

      private string _subTaskDescription;
        public string StepDescription
        {
          get
          {
            return _subTaskDescription;
          }
          private set
          {
            if (value == null)
            {
              _subTaskDescription = string.Empty;
            }
            else
            {
              _subTaskDescription = value;
            }
          }
        }

        public void OnTaskStarted(int subTaskCount)
        {
          _progressBar.Invoke(new MethodInvoker(InitializeProgressbar));
          _cancelButton.Invoke(new MethodInvoker(ActivateCancelButton));

          StepCount = subTaskCount;
          StepDescription = string.Empty;

          Invoke(new MethodInvoker(Show));
        }

        public void OnStepStarted(string description)
        {
          StepDescription = description;
        }

        public void OnStepFinished()
        {
          StepDescription = string.Empty;
          StepsCompletedCount++;
          if (InvokeRequired)
          {
            Invoke(new MethodInvoker(DisplayStatus));
          }
          else
          {
            DisplayStatus();
          }
        }

        public void CancelTask()
        {
          IsCancelled = true;
          OnCancelled(new EventArgs());
        }

        public void OnTaskFinished()
        {
          StepDescription = string.Empty;
          StepsCompletedCount = StepCount;
          Invoke(new MethodInvoker(Hide));
          OnFinished(new EventArgs());
        }

        #endregion
      }
}